import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import {randomMath} from '@/util/util'

/** ****************************************************************/
import Home from '@/views/Home/Home.vue'
import Photots from "@/views/Photos/Photots.vue"
import Video from "@/views/Video/Video.vue";
import Card from '@/views/Card/Card.vue'
/***************************************************************** */

const routes: Array<RouteRecordRaw> = [
  {
    path:'/',
    redirect:'/home'
  },
  {
    path:'/home',
    component:Home,
    meta:{
      title:'首页'
    }
  },{
    path:'/photos',
    component:Photots,
    meta:{
      title: '图集'
    }
  },{
    path:'/video',
    component:Video,
    meta:{
      title:'视频'
    }
  },{
    path:'/card',
    component: Card,
    meta:{
      title:'幸运翻盘'
    }
  }
]

const router = createRouter({
  history: createWebHashHistory(process.env.BASE_URL),
  routes
})


// 设置自动播放音乐名单
let musicWhrite:Array<string> = ['/photos']

router.beforeEach((to,from,next) => {
  let loadingViewDom = document.querySelector('.mask') as HTMLElement
  let App = document.querySelector('#app') as HTMLElement
  loadingViewDom.style.display = 'block'

  let done = musicWhrite.indexOf(to.path) > -1
  if(done) {
    let audio = document.querySelector('.cus-audio') as HTMLAudioElement
    let isPlay = !audio.paused
    if(!isPlay) {
      let playPromise = audio.play()
      console.log(playPromise)
      if(playPromise) {
        playPromise.then((res:any) => {

        }).catch((reason) => {
          // 错误捕获处理
          console.log(reason)

          audio.play()
        })
      }
    }
  }
   //上线开启
   next()
  //  setTimeout(() => {
  //    next()
  //  },randomMath(1000,3000))
})

router.afterEach((to,from,next) => {
  let loadingViewDom = document.querySelector('.mask') as HTMLElement
  let App = document.querySelector('#app') as HTMLElement
  loadingViewDom.style.display = 'none'
  document.title = to.meta.title as string
})

export default router
